let baseURL = "http://192.168.10.10:10000/api";

function getContentType(options) {
	if(options.method?.toLocaleUpperCase() == "POST" || options.method?.toLocaleUpperCase() == "DELETE"){
		return options.contentType? {["content-type"]: options.contentType} : {}
	}else{
		return {}
	}
}

export const request = (options) => {
	return new Promise((resolve, reject) => {
		console.log('请求参数', options)
		console.log('请求地址', baseURL + options.url)
		uni.request({
			url: baseURL + options.url,
			method: options.method || "GET",
			data: options.data || options.params || {},
			dataType: options.dataType || "json",
			responseType: options.responseType || "text",
			enableChunked: options.enableChunked || false,
			header: {
				devtype: "app", //自定义请求头
				Authorization: uni.getStorageSync("token") && options.url != "/account/phoneLogin" ? uni.getStorageSync("token") : "", //从本地缓存获取token
				...getContentType(options) //动态获取Content-Type
			},
			//请求成功
			success: (res) => {
				if(res.data.code != 0 && res.data.code != 1){
					console.error('请求失败', res)
					uni.showToast({
						title: res.data.msg || '服务端出错！',
						icon: "none",
						duration: 1000
					})
					reject(res.data.msg || '请求失败！')
				}else{
					console.log('请求成功', res.data)
					resolve(res.data)
				}
			},
			//请求失败回调
			fail: (error) => {
				console.log('http进入错误', error)
				reject(error)
			}
		})
	})
}